package com.cpecc.mail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.WuZi;

public class SendMailThread1 implements Runnable{

	private Email email;
	
	private User user;
	
	private YouJian youJian;
	
	private WuZi wuZi;
	
	private String root;

	public SendMailThread1(Email email, User user, YouJian youJian, WuZi wuZi) {
		super();
		this.email = email;
		this.user = user;
		this.youJian = youJian;
		this.wuZi = wuZi;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	@Override
	public void run() {
		String content=youJian.getContent();
		content=content.replace("{资产编号}", wuZi.getZiChanBH());
		content=content.replace("{物资名称}", wuZi.getWuZiMC());
		content=content.replace("{品牌}", wuZi.getPinPai());
		content=content.replace("{规格型号}", wuZi.getGuiGeXH());
		content=content.replace("{产品序列号}", wuZi.getChanPinXLH());
		content=content.replaceAll(" ", "&nbsp;");
		content=content.replaceAll("\n", "<br>");
		if(youJian.getId()==3){
			String access_url="<a href=\""+root+"/youjian/accessPanDian.do?userId="+user.getId()+"\">进入</a>";
			content=content.replace("{access_url}", access_url);
			String agree_url="<a href=\""+root+"/youjian/agreePanDian.do?id="+wuZi.getId()+"&userId="+user.getId()+"\">同意</a>";
			content=content.replace("{agree_url}", agree_url);
		}
		String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", email.getMailServerHost());
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.ssl.enable", "true");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", email.getMailServerPort());
		props.setProperty("mail.smtp.socketFactory.port", email.getMailServerPort());
		props.put("mail.smtp.auth", "true");
		Session session = Session.getDefaultInstance(props,
				new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(email.getUserName(), email.getPassword());
					}
				});
		try {
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			// 设置HTML内容
			html.setContent(content, "text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(email.getUserName()));
			msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(user.getEmail(), false));
			msg.setSubject(youJian.getTitle());
			msg.setContent(mainPart);
			msg.setSentDate(new Date());
			Transport.send(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
